package com.hebrf.sdk.web;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hebrf.sdk.R;
import com.tamic.jswebview.browse.JsWeb.CustomWebViewClient;
import com.tamic.jswebview.view.ProgressBarWebView;

import java.util.Map;

public class WebActivity extends AppCompatActivity {

    public static final String TITLE="WebActivity.webTitle";
    public static final String URL="WebActivity.webUrl";
    /**
     * 详细内容请看 https://github.com/Tamicer/JsWebView
     */
    ProgressBarWebView mProgressBarWebView;
    TextView mTVTitle;
    ImageView mIVBack;
    Intent intent;
    private String title;
    private String url;
    CustomWebViewClient customWebViewClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        initData();
        intView();
        loadUrl();
    }
    /**
     * 初始化数据
     */
    private void initData() {
        intent=getIntent();
        if (intent==null) finish();
        //获取标题
        title=intent.getStringExtra(TITLE);
        //获取Uri
        url=intent.getStringExtra(URL);
        if (title==null||url==null) finish();

    }

    /**
     * 初始化View
     */
    private void intView() {
        //初始化标题栏
        mTVTitle = (TextView) findViewById(R.id.tv_name);
        mTVTitle.setText(title);
        mIVBack = (ImageView) findViewById(R.id.iv_back);
        mIVBack.setOnClickListener((view)->
            finish()
        );


        //初始化web
        mProgressBarWebView  = (ProgressBarWebView) findViewById(R.id.progress_webview);
        customWebViewClient  =new CustomWebViewClient(mProgressBarWebView.getWebView()) {
            @Override
            public String onPageError(String url) {
                //指定网络加载失败时的错误页面
                return null;
            }

            @NonNull
            @Override
            public Map<String, String> onPageHeaders(String url) {
                // 可以加入header
                return null;
            }
        };
        mProgressBarWebView.setWebViewClient(customWebViewClient);


    }
    /**
     * 加载网址
     */
    private void loadUrl() {
        mProgressBarWebView.loadUrl(url);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mProgressBarWebView.getWebView().destroy();

    }
}
